#ifndef __M_UTIL_H__
#define __M_UTIL_H__
#include <mysql/mysql.h>
#include <string>
#include <fstream>
#include <jsoncpp/json/json.h>
#include "Log.hpp"
using namespace std;

/*MySQL C API工具类*/
class mysql_util {
public:
    /*创建MySQL句柄*/
    static MYSQL *mysql_create(const std::string &host, const std::string &user, const std::string                                      &passwd, const std::string db = "gobang", uint16_t port = 4106) {
        /*初始化MYSQL句柄*/
        MYSQL *mysql = mysql_init(nullptr);
        if(mysql == nullptr) {
            LOG(FATAL, "mysql init failed");
            return nullptr;
        }
        /*连接MySQL数据库*/
        mysql = mysql_real_connect(mysql, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0);
        if(mysql == nullptr) {
            LOG(FATAL, "mysql connect failed: %s", mysql_error(mysql));
            mysql_close(mysql);
            return nullptr;
        }
        /*设置客户端字符集*/
        if(mysql_set_character_set(mysql, "utf8") != 0) {
            LOG(ERROR, "client character set failed: %s", mysql_error(mysql));
        }
        return mysql;
    }

    /*执行sql语句*/
    static bool mysql_execute(MYSQL *mysql, const std::string &sql) {
        if(mysql_query(mysql, sql.c_str()) != 0) {
            LOG(ERROR, "sql query failed: %s", mysql_error(mysql));
            return false;
        }
        return true;
    }

    /*销毁MySQL句柄*/
    static void mysql_destroy(MYSQL *mysql) {
        if(mysql != nullptr) {
            mysql_close(mysql);
        }
    }
};

/*读取文件数据工具类*/
class file_util {
public:
    static bool read(const char* filename, std::string &data) {
        /*以二进制形式打开文件*/
        std::ifstream ifs(filename, std::ios::binary);
        if(ifs.is_open() == false) {
            LOG(ERROR, "open %s file failed", filename);
            return false;
        }
        /*获取文件大小*/
        size_t size;
        ifs.seekg(0, std::ios::end);
        size = ifs.tellg();
        ifs.seekg(0, std::ios::beg);
        /*读取文件内容*/
        data.resize(size);
        ifs.read(&data[0], size);
        if(ifs.good() == false) {
            LOG(ERROR, "read %s file content failed", filename);
            ifs.close();
            return false;
        }
        /*关闭文件*/
        ifs.close();
        return true;
    }
};

/*jsoncpp工具类*/
class json_util {
public:
    /*序列化接口*/
    static bool serialize(Json::Value &root, std::string &str) {
        Json::StreamWriterBuilder swb;
	    swb["emitUTF8"] = true;
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
        std::stringstream ss;
        if(sw->write(root, &ss) != 0) {
            LOG(ERROR, "json serialize failed");
            return false;
        }
        str = ss.str();
        return true;
    }

    /*反序列化接口*/
    static bool deserialize(const std::string &str, Json::Value &root) {
        Json::CharReaderBuilder crb;
        std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
        std::string err;
        if(cr->parse(str.c_str(), str.c_str() + str.size(), &root, &err) == false) {
            LOG(ERROR, "json deserialize failed: %s", err.c_str());
            return false;
        }
        return true;
    }
};


#endif
